package com.study.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.study.intercept.AuthenticateInterceptor;

@Configuration
public class WebConfig implements WebMvcConfigurer {

	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		registry.addViewController("/").setViewName("login/login");
	}

	/**
	 * 自定义拦截器，添加拦截路径和排除拦截路径 addPathPatterns():添加需要拦截的路径
	 * excludePathPatterns():添加不需要拦截的路径
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		List<String> list = new ArrayList<>();
		list.add("/auth/login");
		list.add("/auth/logout");
		list.add("/webjars/**");
		list.add("/js/**");
		list.add("/");
		list.add("/favicon.ico");
		list.add("/error");
		registry.addInterceptor(new AuthenticateInterceptor()).addPathPatterns("/**").excludePathPatterns(list);
	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		// 静态资源放行
		// 可以访问localhost:8080/static/images/image.jpg
		// registry.addResourceHandler("/static/files/**").addResourceLocations("classpath:/static/files/");
		// registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
	}

}
